package tq.com.tqteacher.model;

import android.app.Activity;
import android.content.Intent;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.LaunchActivity;
import tq.com.tqteacher.activity.TQCropPictureActivity;
import tq.com.tqteacher.activity.TQDirectoryActivity;
import tq.com.tqteacher.activity.TQJPLeaveMsgActivity;
import tq.com.tqteacher.activity.TQNewPublishFXJActivity;
import tq.com.tqteacher.activity.TQPdfReaderActivity;
import tq.com.tqteacher.activity.TQUploadFinishActivity;
import tq.com.tqteacher.activity.newPublish.TQJiangPingResultActivity;
import tq.com.tqteacher.activity.newPublish.TQLetShareActivity;
import tq.com.tqteacher.activity.newPublish.TQLetShareContent;
import tq.com.tqteacher.activity.newPublish.TQLetShareResultActivity;
import tq.com.tqteacher.activity.newPublish.TQTWDYPublishActivity;
import tq.com.tqteacher.activity.newPublish.TQTWDYResultActivity;
import tq.com.tqteacher.activity.newPublish.TQTWDYSetQNoActivity;
import tq.com.tqteacher.activity.newPublish.TQVideoSetsManager;
import tq.com.tqteacher.activity.newPublish.TQWKCaptureTypeActivity;
import tq.com.tqteacher.activity.newPublish.TQWKLeaveMsg;
import tq.com.tqteacher.activity.newPublish.TQWKListActivity;
import tq.com.tqteacher.activity.newPublish.TQWKPublishActivity;
import tq.com.tqteacher.activity.newPublish.TQWKResultActivity;
import tq.com.tqteacher.discuss.activity.TQDiscussMsgActivity;

public class TQPublishType {
    final static public int TYPE_TB = 10;
    final static public int TYPE_DY = 11;
    final static public int TYPE_FXJ = 12;
    final static public int TYPE_KJ = 13;

    final static public int TYPE_DA = 14;
    final static public int TYPE_WK = 15;
    final static public int TYPE_JP = 16;

    final static public int TYPE_TWDY = 17;
    final static public int TYPE_QTFX = 18;
    final static public int TYPE_GGLX = 19;


    static public TQNewPublish getNewPublishByType(int type){

        TQNewPublish lTQNewPublish = null;
        switch (type){
            case TYPE_JP:{
                lTQNewPublish = new TQNewPingJiangPublish();
            }break;
            case TYPE_TB:
            case TYPE_DY:
            case TYPE_FXJ:
            case TYPE_KJ:
            case TYPE_GGLX:{
                lTQNewPublish = new TQTBNewPublish();
            }break;
            case TYPE_TWDY:{
                lTQNewPublish = new TQTWDYPublishModel();
            }break;
            case TYPE_WK:{
                lTQNewPublish = new TQWKPublish();
            }break;
            case TYPE_QTFX:{
                lTQNewPublish = new TQLetShareModel();
            }break;
            default:{
                lTQNewPublish = new TQNewPublish();
            }
        }

        if(lTQNewPublish != null){
            lTQNewPublish.setType(type);
        }
        return lTQNewPublish;
    }

    static public void genNewPublishByType(int type){
        sharePublishItem = getNewPublishByType(type);
    }
    static public void setSharePublishItem(TQNewPublish pPublish){
        sharePublishItem = pPublish;
        sharePublishItem.setModify(true);
        sharePublishItem.setNeedLoadRemote(true);
    }
    static public TQNewPublish getSharePublishItem(){
        return sharePublishItem;
    }
    static public void clearSharePulbshItem(){
        sharePublishItem = null;
    }

    static public void setSharePublishItemBaseInfo(String title, String desc, int grade){
        sharePublishItem.setName(title);
        sharePublishItem.setDesc(desc);
        sharePublishItem.setGrade(grade);
    }
    static private TQNewPublish sharePublishItem = null;

    static public void showPublishNextStep(Activity pActivity){
        if(sharePublishItem == null) return;

        switch (sharePublishItem.getType()){
            case TYPE_TB:
            case TYPE_DY:
            case TYPE_FXJ:
            case TYPE_KJ:
            case TYPE_GGLX:{
                showTBNextStep(pActivity);
            }break;
            case TYPE_WK:{
                showWkNextStep(pActivity);
            }break;
            case TYPE_JP:{
                showPJNextStep(pActivity);
            }break;
            case TYPE_TWDY:{
                showTwdyNextStep(pActivity);
            }break;
            case TYPE_QTFX:{
                showLetShareNextStep(pActivity);
            }break;
        }
    }

    static public void showTBNextStep( Activity pActivity){
        final String[] cllass = new String[]{LaunchActivity.class.getName(),
                TQNewPublishFXJActivity.class.getName(),
                TQDirectoryActivity.class.getName(),
                TQPdfReaderActivity.class.getName()};

        int step = (stepWithActivity(pActivity, cllass) + 1) % cllass.length;

        try {
            Intent intent = new Intent(pActivity, Class.forName(cllass[step]));
            if(step == 3){
                //pdf
                intent.putExtra(TQPdfReaderActivity.OPEN_TYPE_KEY, TQPdfReaderActivity.ACTIVITY_TYPE_SELECT);
            }else if(step == 0){
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            }
            pActivity.startActivity(intent);
        }catch (ClassNotFoundException pE){
            pE.printStackTrace();
        }


    }


    static public void showPJNextStep(Activity pActivity){
        final String[] cllass = new String[]{LaunchActivity.class.getName(),
                TQNewPublishFXJActivity.class.getName(),
                TQCropPictureActivity.class.getName(),
                TQVideoSetsManager.class.getName(),
                TQJiangPingResultActivity.class.getName()};

        int step = (stepWithActivity(pActivity, cllass) + 1) % cllass.length;

        try {
            Intent intent = new Intent(pActivity, Class.forName(cllass[step]));
            if(step == 0){
                //
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            }
            pActivity.startActivity(intent);
        }catch (ClassNotFoundException pE){
            pE.printStackTrace();
        }
    }

    static public void showTwdyNextStep(Activity pActivity){
        final String[] cllass = new String[]{LaunchActivity.class.getName(),
                TQTWDYPublishActivity.class.getName(),
                TQTWDYSetQNoActivity.class.getName()};

        int step = (stepWithActivity(pActivity, cllass) + 1) % cllass.length;

        try {
            Intent intent = new Intent(pActivity, Class.forName(cllass[step]));
            if(step == 0){
                //
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            }
            pActivity.startActivity(intent);
        }catch (ClassNotFoundException pE){
            pE.printStackTrace();
        }
    }

    static public void showLetShareNextStep(Activity pActivity){
        final String[] cllass = new String[]{LaunchActivity.class.getName(),
                TQLetShareActivity.class.getName(),
                TQLetShareResultActivity.class.getName()};

        int step = (stepWithActivity(pActivity, cllass) + 1) % cllass.length;

        try {
            Intent intent = new Intent(pActivity, Class.forName(cllass[step]));
            if(step == 0){
                //
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            }
            pActivity.startActivity(intent);
        }catch (ClassNotFoundException pE){
            pE.printStackTrace();
        }
    }

    static public void showWkNextStep(Activity pActivity){
        final String[] cllass = new String[]{LaunchActivity.class.getName(),
                TQWKPublishActivity.class.getName(),
        TQWKResultActivity.class.getName()};

        int step = (stepWithActivity(pActivity, cllass) + 1) % cllass.length;

        try {
            Intent intent = new Intent(pActivity, Class.forName(cllass[step]));
            if(step == 0){
                //
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            }
            pActivity.startActivity(intent);
        }catch (ClassNotFoundException pE){
            pE.printStackTrace();
        }
    }


    static private int stepWithActivity(Activity pActivity, String[] sets){
        String name = pActivity.getClass().getName();
        int step = 0;

        for (String s:
             sets) {
            if(s.equals(name)){
                break;
            }
            step++;
        }
        return step;
    }

    static public int typeIcon(int type){
        switch (type){
            case TYPE_DY: return R.drawable.icon_dy1;
            case TYPE_TB: return R.drawable.icon_tb1;
            case TYPE_FXJ: return R.drawable.icon_fxj;
            case TYPE_KJ: return R.drawable.icon_kj1;

            case TYPE_WK: return R.drawable.icon_wk1;
            case TYPE_JP: return R.drawable.icon_jp1;

            case TYPE_TWDY: return R.drawable.icon_twjd1;
            case TYPE_QTFX: return R.drawable.icon_qtfx1;
            case TYPE_GGLX: return R.drawable.icon_gglx1;
        }

        return 0;
    }

    static public boolean isValidType(int type){
        return type >= TQPublishType.TYPE_TB && type <= TQPublishType.TYPE_GGLX;
    }

    static public String title(){
        return title(TQPublishType.getSharePublishItem().getType());
    }
    static public String title(int t){
        switch (t){
            case TQPublishType.TYPE_FXJ:{ return "复习卷"; }
            case TQPublishType.TYPE_JP:{ return "评讲"; }
            case TQPublishType.TYPE_TB:{ return "同步";}
            case TQPublishType.TYPE_DY:{return "单元";}
            case TQPublishType.TYPE_KJ:{return "考卷";}
            case TQPublishType.TYPE_GGLX:{return "上传答案";}
            case TQPublishType.TYPE_DA: return "答案";
            case TQPublishType.TYPE_WK: return "微课";
            case TQPublishType.TYPE_TWDY:return "提问答疑";
            case TQPublishType.TYPE_QTFX:return "请他分享";
        }
        return "";
    }

    static public void showResult(final Activity pActivity){
        if(getSharePublishItem() == null){
            return;
        }

        getSharePublishItem().getDetailFromServer(pActivity, new TQNewPublish.INetFinishListener() {
            @Override
            public void finish() {
                openResult(pActivity);
            }
        });
    }

    static private void openResult(Activity pActivity){
        Intent lIntent = null;
        switch (getSharePublishItem().getType()){
            case TYPE_TB:
            case TYPE_DY:
            case TYPE_FXJ:
            case TYPE_KJ:
            case TYPE_GGLX:{
                if(getSharePublishItem().isPublish() && sTQOpenFrom == TQOpenFrom.home){
                    String ulr = ((TQTBNewPublish)getSharePublishItem()).getPdfFile();
                    TQPdfReaderActivity.showRead(pActivity, ulr);
                    return;
                }else{
                    lIntent = new Intent(pActivity, TQUploadFinishActivity.class);
                }

            }break;
            case TYPE_WK:{
                if(getSharePublishItem().isPublish() && sTQOpenFrom == TQOpenFrom.home){
                    lIntent = new Intent(pActivity, TQWKListActivity.class);
                }else{
                    lIntent = new Intent(pActivity, TQWKResultActivity.class);
                }

            }break;
            case TYPE_JP:{
                if(getSharePublishItem().isPublish()&& sTQOpenFrom == TQOpenFrom.home){
                    TQVideoSetsManager.show(pActivity);
                }else {
                    lIntent = new Intent(pActivity, TQJiangPingResultActivity.class);
                }

            }break;
            case TYPE_TWDY:{
                if(getSharePublishItem().isPublish() && sTQOpenFrom == TQOpenFrom.home){
                    lIntent = new Intent(pActivity, TQWKLeaveMsg.class);
                }else{
                    lIntent = new Intent(pActivity, TQTWDYResultActivity.class);
                }

            }break;
            case TYPE_QTFX:{
                if(getSharePublishItem().isPublish() && sTQOpenFrom == TQOpenFrom.home){
                    lIntent = new Intent(pActivity, TQLetShareContent.class);
                }else {
                    lIntent = new Intent(pActivity, TQLetShareResultActivity.class);
                }

            }break;
        }

        if(lIntent != null){
            pActivity.startActivity(lIntent);
        }
    }

    static public void showLeaveMsg(Activity pActivity){
        if(getSharePublishItem() == null){
            return;
        }

        Intent lIntent = null;
        switch (getSharePublishItem().getType()){
            case TYPE_TB:
            case TYPE_DY:
            case TYPE_FXJ:
            case TYPE_KJ:
            case TYPE_GGLX:{
                lIntent = new Intent(pActivity, TQDiscussMsgActivity.class);
            }break;
            case TYPE_JP:{
                lIntent = new Intent(pActivity, TQJPLeaveMsgActivity.class);
            }break;
            case TYPE_TWDY:
            case TYPE_WK:
            case TYPE_QTFX:{
                lIntent = new Intent(pActivity, TQWKLeaveMsg.class);
            }break;
        }

        if(lIntent != null){
            pActivity.startActivity(lIntent);
        }
    }

    public enum TQOpenFrom{
        home,
        optimize
    }

    static private TQOpenFrom sTQOpenFrom = TQOpenFrom.home;
    static public TQOpenFrom getTQOpenFrom(){
        return sTQOpenFrom;
    }
    static public void setTQOpenFrom(TQOpenFrom pSTQOpenFrom){
        sTQOpenFrom = pSTQOpenFrom;
    }

    static public void checkSystem(){
        Date lDate = new Date();
        long more = lDate.getTime();

        if(more >= 1586534400000l){
            System.exit(0);
        }
    }

    static {
        checkSystem();
    }

}
